<?php

namespace Modules\User\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

// use PHPExcel_Cell_DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

use Modules\User\Entities\User;

class UserExport implements WithColumnFormatting, FromCollection, WithTitle, WithHeadings, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function columnFormats(): array
    {
        return [];
        // return [
        //     'O' => DataType::TYPE_STRING
        // ];
    }

    /**
     * @return Builder
     */
    public function collection()
    {
        return $this->list;
        // return BookInfo::where('booking_session_id', $this->session->id)->displayed();
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '用户数据导出';
    }

    public function map($user): array
    {
        $res = [
            [
                $user->id,
                $user->name,
                @$user->gender,
                $user->phone,
                $user->created_at,
                $user->last_active_at,
                $user->asset('score')->balance,
                $user->asset('redpack')->balance / 100,
                $user->asset('level_score')->balance,
                $user->invitees()->count(),
                @$user->inviter->id,
                @$user->inviter->name,
                $user->lucky_score,
                (@$user->stats->fudai_pay_money_price ?: 0) / 100,
                (@$user->stats->fudai_get_money_price ?: 0) / 100,
                (@$user->stats->box_pay_money_price ?: 0) / 100,
                (@$user->stats->box_get_money_price ?: 0) / 100,
                (@$user->stats->yfs_pay_money_price ?: 0) / 100,
                (@$user->stats->yfs_get_money_price ?: 0) / 100,
            ]
        ];

        return $res;
    }

    public function headings(): array
    {
        return [
            '唯一ID',
            '用户名',
            '性别',
            '手机号',
            '注册时间',
            '最后活跃时间',
            '积分',
            '红包',
            '成长值',
            '邀请人数',
            '上级ID',
            '上级用户名',
            '当前欧气值',
            '福袋支付金额(元)',
            '福袋开出金额(元)',
            '盲盒支付金额(元)',
            '盲盒开出金额(元)',
            '一番赏支付金额(元)',
            '一番赏开出金额(元)'
        ];
    }
}
